package com.spring.sunflower.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ScreenUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.spring.sunflower.R;
import com.spring.sunflower.common.WalletActivity;
import com.spring.sunflower.event.LuckyMatchEvent;
import com.spring.sunflower.util.DisplayUtils;

import java.util.Random;

/**
 * Created by <a href="http://blog.csdn.net/student9128">Kevin</a> on 2018/5/8.
 * <h3>
 * Describe:
 * <h3/>
 */
public class DialogManager {
    public static BaseDialog createProgressDialog(String title) {
        return CommonLoadingDialog.newInstance(title);
    }

    private static DialogManager mInstance;
    public static DialogManager getInstance() {
        if (mInstance == null) {
            synchronized (DialogManager.class) {
                if (mInstance == null) {
                    mInstance = new DialogManager();
                }
            }
        }
        return mInstance;
    }

    //    public static BaseDialog createFingerprintDialog() {
//        return FingerprintDialog.newInstance();
//    }
    public void showNeedChargeDialog(Context context) {
        NoticeDialog dialog = new NoticeDialog(context, new OnPermissionIntroClickListener() {
            @Override
            public void onClickCancel() {
            }

            @Override
            public void onClickOK() {
                Intent intent = new Intent(context, WalletActivity.class);
                context.startActivity(intent);
            }
        });
        dialog.setOnCreateViewListener(new NoticeDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText("余额提醒");
                tvDesc.setText("当前余额不足，无法进行该操作");
                tvDesc.setTextColor(ContextCompat.getColor(context, R.color.red_2));
                TextView tvOK = dialog.findViewById(R.id.tvOK);
                tvOK.setText("去充值");
            }
        });
        new XPopup.Builder(context)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
    }
    /**
     * 关注弹窗
     *
     * @param context
     * @param listener
     */
    public void showFollowedDialog(Context context, FollowedDialog.OnFollowedListener listener) {
        FollowedDialog dialog = new FollowedDialog(context);
        dialog.setOnFollowedListener(listener);
        new XPopup.Builder(context)
                .isRequestFocus(false)
                .dismissOnTouchOutside(true)
                .offsetY(220)
                .hasShadowBg(false)
                .popupAnimation(PopupAnimation.TranslateAlphaFromTop)
                .autoDismiss(false)
                .asCustom(dialog)
                .show();

    }

    /**
     * 客服弹窗
     *
     * @param context
     * @param listener
     */
    public void showCustomerDialog(Context context, CustomerDialog.OnCustomerListener listener) {
        CustomerDialog dialog = new CustomerDialog(context);
        dialog.setOnCustomerListener(listener);
        new XPopup.Builder(context)
                .isRequestFocus(false)
                .dismissOnTouchOutside(true)
                .offsetY(220)
                .hasShadowBg(false)
                .popupAnimation(PopupAnimation.TranslateAlphaFromTop)
                .autoDismiss(false)
                .asCustom(dialog)
                .show();

    }

    public void showLuckyMatchDialog(Activity context, LuckyMatchEvent event, String type) {
        LuckyMatchDialog dialog = new LuckyMatchDialog(context, event, type);
        new XPopup.Builder(context)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .offsetY(200)
                .popupAnimation(PopupAnimation.TranslateAlphaFromTop)
                .autoDismiss(false)
                .asCustom(dialog)
                .show();
    }

    public void showPermissionIntroDialog(Context context, String title, String desc, @Nullable OnPermissionIntroClickListener listener) {
        PermissionIntroDialog dialog = new PermissionIntroDialog(context, R.layout.dialog_permission_intro, listener);
        dialog.setOnCreateViewListener(new PermissionIntroDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText(title);
                tvDesc.setText(desc);
            }
        });
        new XPopup.Builder(context)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
    }
    public void showPermissionIntroDialog(Context context, String[] titleArr, String[] descArr, OnPermissionIntroClickListener listener) {
        PermissionIntroDialog dialog = new PermissionIntroDialog(context, R.layout.dialog_permission_intro_list, listener);
        dialog.setOnCreateViewListener(new PermissionIntroDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                LinearLayout container = dialog.getContainer();
                for (int i = 0; i < titleArr.length; i++) {
                    View inflate = View.inflate(context, R.layout.dialog_permission_intro_content_item, null);
                    TextView tvTitle = inflate.findViewById(R.id.tvTitle);
                    TextView tvDesc = inflate.findViewById(R.id.tvDesc);
                    tvTitle.setText(titleArr[i]);
                    tvDesc.setText(descArr[i]);
                    container.addView(inflate);
                }
            }
        });


        new XPopup.Builder(context)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(dialog)
                .show();
    }
    public PopupWindow showGuideVideoDialog(Context context, View view) {
        String[] strArr = new String[]{"喜欢TA，不如直接看看TA吧...",
                "想了解TA？直接视频聊聊天...",
                "心动不如行动，面对面跟TA交流..."};
        Random random = new Random();
        int nextInt = random.nextInt(strArr.length);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        int top = view.getTop();
        View inflate = View.inflate(context, R.layout.dialog_user_guide_video, null);
        TextView tv = inflate.findViewById(R.id.tvContent);
        tv.setText(strArr[nextInt]);
        PopupWindow popupWindow = new PopupWindow(inflate);
        popupWindow.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        int height1 = DisplayUtils.dip2px(context, 60);
        popupWindow.setHeight(height1);
        popupWindow.setOutsideTouchable(true);
//        popupWindow.update();
        popupWindow.setAnimationStyle(R.style.popAnim);
        popupWindow.showAtLocation(view, Gravity.TOP, 0, y - popupWindow.getHeight());

        return popupWindow;
    }
}
